﻿Imports System.Data.SqlClient
Public Class clsCatalogoBombas
    Private mintIdBomba As Integer
    Private mstrNombre As String
    Private mstrUbicacion As String
    Private mdblNumeracion_Actual As Double
    Private mintIdAlmacen As Integer
    Public Property IdBomba As Integer
        Get
            Return mintIdBomba
        End Get
        Set(value As Integer)
            mintIdBomba = value
        End Set
    End Property
    Public Property Nombre As String
        Get
            Return mstrNombre
        End Get
        Set(value As String)
            mstrNombre = value
        End Set
    End Property
    Public Property Ubicacion As String
        Get
            Return mstrUbicacion
        End Get
        Set(value As String)
            mstrUbicacion = value
        End Set
    End Property
    Public Property Numeracion_Actual As Double
        Get
            Return mdblNumeracion_Actual
        End Get
        Set(value As Double)
            mdblNumeracion_Actual = value
        End Set
    End Property
    Public Property IdAlmacen As Integer
        Get
            Return mintIdAlmacen
        End Get
        Set(value As Integer)
            mintIdAlmacen = value
        End Set
    End Property
    
    Public Sub New()
        mintIdBomba = 0
        mstrNombre = ""
        mstrUbicacion = ""
        mdblNumeracion_Actual = 0
        mintIdAlmacen = 0
       
    End Sub
    Public Sub Guardar()
        If IdBomba = 0 Then
            Exit Sub
        End If

        Dim intRetVal As Integer = BuscarIntDatoCitra("Bombas", "IdBomba", "IdBomba=" & IdBomba)

        If intRetVal = 0 Then
            Insertar()
        Else
            Actualizar()
        End If
    End Sub
    Public Sub Buscar()
        Dim cnConn As New SqlConnection
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = CitraConnection
            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = "SELECT * FROM Bombas WHERE Nombre='" & Nombre & "'"

            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            If rdBuscar.Read() Then
                mstrNombre = rdBuscar("Nombre").ToString.Trim
                mstrUbicacion = rdBuscar("Ubicacion").ToString.Trim
                mdblNumeracion_Actual = rdBuscar("Numeracion_Actual")
                mintIdAlmacen = rdBuscar("IdAlmacen")
            Else
                mstrNombre = ""
                mstrUbicacion = ""
                mdblNumeracion_Actual = 0
                mintIdAlmacen = 0
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try

    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into Bombas (IdBomba," & _
                            "Nombre," & _
                            "Ubicacion," & _
                            "Numeracion_Actual," & _
                            "IdAlmacen) " & _
                            "VALUES (" & _
                            "@IdBomba," & _
                            "@Nombre," & _
                            "@Ubicacion," & _
                            "@Numeracion_Actual," & _
                            "@IdAlmacen) "


        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdBomba", mintIdBomba)
        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("Ubicacion", mstrUbicacion)
        Command.Parameters.AddWithValue("Numeracion_Actual", mdblNumeracion_Actual)
        Command.Parameters.AddWithValue("IdAlmacen", mintIdAlmacen)
       

        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar la bomba " & mstrNombre, MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub Actualizar()
        Dim strSqlQry As String
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        strSqlQry = "Update Bombas Set " & _
                    " Nombre=@Nombre, " & _
                    " Ubicacion=@Ubicacion, " & _
                    " Numeracion_Actual=@Numeracion_Actual, " & _
                    " IdAlmacen=@IdAlmacen " & _
                    " where IdBomba=@IdBomba"

        Dim Command As New SqlCommand(strSqlQry, cnConn)
        Command.Parameters.AddWithValue("IdBomba", mintIdBomba)
        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("Ubicacion", mstrUbicacion)
        Command.Parameters.AddWithValue("Numeracion_Actual", mdblNumeracion_Actual)
        Command.Parameters.AddWithValue("IdAlmacen", mintIdAlmacen)

        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar la bomba " & mstrNombre, MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
End Class
